package com.norman.sorry.core.cards;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;

import com.norman.sorry.core.moves.IMove;
import com.norman.sorry.core.moves.MoveFactory;
import com.norman.sorry.core.pieces.IPiece;
import com.norman.sorry.core.players.IPlayer;
import com.norman.sorry.core.tiles.ILinkedTile;
import com.norman.sorry.core.tiles.TileUtils;

/**
 * The class representation of the two card. This card allows the player to move
 * any game piece two tiles forward or move a game piece out of start. The
 * player may draw again even if they were not able to move.
 * 
 * @author Brian Norman
 * @version 0.0.1 beta
 * @since 0.0.1 beta
 */
public class Two extends AbstractCard {

   /**
    * Definition of the constant integer two.
    */
   public static final int TWO = 2;

   /**
    * {@inheritDoc}
    * 
    * Returns the moves of each game piece moving two tiles forward that can and
    * moving each game piece that is in start out.
    * 
    * @see com.norman.sorry.core.cards.ICard#getMoves(IPlayer, Map)
    */
   @Override
   public Collection<IMove> getMoves(IPlayer player, Map<IPiece, ILinkedTile> pieces) {
      Collection<IMove> moves = new LinkedList<IMove>();

      for (IPiece p : player.getPieces()) {
         ILinkedTile tile = pieces.get(p);
         IMove m = null;

         if (tile != null && tile.isHome()) {
            tile = tile.getNext(p.getColor());
         } else {
            tile = TileUtils.getNext(tile, p.getColor(), TWO);
         }

         m = MoveFactory.createSingleMove(p, tile);
         if (m != null) {
            moves.add(m);
         }
      }

      return moves;
   }
}
